package ch.rheintalconnection.sysventory.technicalServices.hibernate;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.apache.log4j.Logger;

import ch.rheintalconnection.sysventory.domain.Scan;
import ch.rheintalconnection.sysventory.technicalServices.IDataReader;

public class MySqlDataReader implements IDataReader {

	private static EntityManagerFactory emf = null;
	private static EntityManager em = null;
	private static EntityTransaction tx = null;

	private Logger logger;

	public MySqlDataReader(Logger logger) {
		super();
		this.logger = logger;

		emf = Persistence
				.createEntityManagerFactory("ch.rheintalconnection.sysventory");
		em = emf.createEntityManager();

		tx = em.getTransaction();
	}

	public List<Scan> selectAll() {

		tx.begin();
		List<Scan> result = em.createQuery("from Scan", Scan.class)
				.getResultList();

		tx.commit();
		em.clear();

		return result;
	}

	public Scan selectLast() {
		List<Scan> result = selectAll();
		Scan scan = result.get(result.size()-1);

		return scan;
	}

}
